package com.zztl.infiniteBand.base

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import butterknife.ButterKnife
import butterknife.Unbinder
import com.githang.statusbar.StatusBarCompat
import com.trello.rxlifecycle.components.support.RxAppCompatActivity
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.eventbus.TokenLoseEvent
import com.zztl.infiniteBand.ui.activity.LoginActivity
import com.zztl.infiniteBand.util.ActivityManager
import com.zztl.infiniteBand.util.language.LanguageUtil
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


abstract class BaseActivity : RxAppCompatActivity() {


    private var mBind: Unbinder? = null
    protected var headerView: RelativeLayout? = null
    private var tvTitle: TextView? = null
    private var tvRightTitle: TextView? = null
    private var backView: ImageView? = null
    private var rigthView: ImageView? = null


    //获取status_bar_height资源的ID
    //根据资源ID获取响应的尺寸值
    val statusBarHeight: Int
        get() {
            var statusBarHeight = -1
            val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
            if (resourceId > 0) {
                statusBarHeight = resources.getDimensionPixelSize(resourceId)
            }
            return statusBarHeight
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ActivityManager.getInstance().pushOneActivity(this)
        setContentView(setLayoutId())
        mBind = ButterKnife.bind(this)
        EventBus.getDefault().register(this)
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            StatusBarCompat.setStatusBarColor(this, Color.WHITE, true)
        }
        headerView = findViewById(R.id.head_title)
        if (null != headerView) {
            tvTitle = headerView!!.findViewById(R.id.title)
            backView = headerView!!.findViewById(R.id.btn_back)
            rigthView = headerView!!.findViewById(R.id.iv_right)
            tvRightTitle = headerView!!.findViewById(R.id.tv_right_title)
            backView!!.setOnClickListener { finish() }
            rigthView!!.setOnClickListener { clickRightImg() }
            tvRightTitle!!.setOnClickListener { clickRightTv() }
        }
        initBase()
    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(LanguageUtil.setLocal(newBase!!))
    }

    fun setTitle(title: String) {

        tvTitle?.text = title

    }

    fun setBackViewVisible(visible: Boolean) {

        backView?.visibility = if (visible) View.VISIBLE else View.GONE

    }

    fun setRightImgViewVisible(visible: Boolean) {

        rigthView?.visibility = if (visible) View.VISIBLE else View.GONE

    }


    fun setRightTvViewVisible(visible: Boolean) {

        tvRightTitle?.visibility = if (visible) View.VISIBLE else View.GONE

    }

    fun setRightTitle(title: String) {

        tvRightTitle?.text = title

    }

    protected fun clickRightImg() {}

    protected open fun clickRightTv() {}


    protected abstract fun setLayoutId(): Int

    protected abstract fun initBase()


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun Event(event: TokenLoseEvent) {
        skipActivity(this, LoginActivity::class.java)
    }

    override fun onDestroy() {
        mBind?.unbind()
        EventBus.getDefault().unregister(this)
        super.onDestroy()

    }

    /**
     * skip to @param(cls)ï¼Œand call @param(aty's) finish() method
     */
    fun skipActivity(aty: Activity, cls: Class<*>) {
        showActivity(aty, cls)
        finish()
    }

    /**
     * skip to @param(cls)ï¼Œand call @param(aty's) finish() method
     */
    fun skipActivity(it: Intent) {
        showActivity(it)
        finish()
    }

    /**
     * skip to @param(cls)ï¼Œand call @param(aty's) finish() method
     */
    fun skipActivity(aty: Activity, cls: Class<*>, extras: Bundle) {
        showActivity(aty, cls, extras)
        finish()
    }

    /**
     * show to @param(cls)ï¼Œbut can't finish activity
     */
    fun showActivity(aty: Activity, cls: Class<*>) {
        val intent = Intent()
        intent.setClass(aty, cls)
        startActivity(intent)
    }

    /**
     * show to @param(cls)ï¼Œbut can't finish activity
     */
    fun showActivity(it: Intent) {
        startActivity(it)

    }

    /**
     * show to @param(cls)ï¼Œbut can't finish activity
     */
    fun showActivity(aty: Activity, cls: Class<*>, extras: Bundle) {
        val intent = Intent()
        intent.putExtras(extras)
        intent.setClass(aty, cls)
        startActivity(intent)

    }


    /**
     * @param aty
     * @param cls
     * @param requestCode
     */
    fun showActivityForResult(aty: Activity, cls: Class<*>, requestCode: Int) {
        val intent = Intent()
        intent.setClass(aty, cls)
        startActivityForResult(intent, requestCode)
    }

    /**
     * @param i
     * @param requestCode
     */
    fun showActivityForResult(i: Intent, requestCode: Int) {
        startActivityForResult(i, requestCode)
    }


    override fun finish() {
        ActivityManager.getInstance().activityStack.remove(this)
        super.finish()

    }


}